package com.zzzzzz.oms.timeLimit;

import java.util.List;
import java.util.Map;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import com.zzzzzz.plugins.jdbctemplate.BaseDao;
import com.zzzzzz.plugins.jdbctemplate.Finder;
import com.zzzzzz.plugins.jdbctemplate.DaoUtils;
import com.zzzzzz.utils.page.Page;
import com.zzzzzz.plugins.shiro.I;

/**
 * @author hing
 * @version 1.0.0
 */
@Repository
public class TimeLimitDao {
	
	@Resource
	private BaseDao baseDao;
	
	//SQL 增、删、查、改
	private final static String sql_add = "insert into t_timeLimit(cd,clientId, name, days, descr, addDt, addBy, st) values(:cd,:clientId, :name, :days,:descr, :addDt, :addBy, :st)";
	private final static String sql_updStByIds = "update t_timeLimit set st = :st, updDt = :updDt, updBy = :updBy where id in (:ids)";
	private final static String sql_upd = "update t_timeLimit set cd=:cd,clientId=:clientId, name=:name, days=:days,descr=:descr, updDt=:updDt, updBy=:updBy, st=:st where id = :id";
	private final static String sql_findById = "select id, cd, clientId,name, days,descr, addDt, addBy, updDt, updBy, st from t_timeLimit where id = :id";
	
	// 新增
	public Long add(TimeLimit timeLimit){
		return baseDao.updateGetLongKey(sql_add, timeLimit);
	}
	
	// 停用数据
	public int updStByIds(List<Long> ids, Integer st, I i){
		Finder finder = new Finder(sql_updStByIds).setParam("ids", ids).setParam("st", st)
		.setParam("updDt", new Date()).setParam("updBy", i.getId());
		return baseDao.update(finder);
	}		
	
	// 编辑
	public int updById(TimeLimit driver){
		return baseDao.update(sql_upd, driver);
	}
	
	// 查找一条数据
	public TimeLimit findById(Long id){
		Finder finder = new Finder(sql_findById).setParam("id", id);
		return baseDao.findOne(finder, TimeLimit.class);
	}
	
	
	// 根据客户和城市名找时效天数
	public Integer findByClientIdAndName(Long clientId,String cityName){
		Finder finder = new Finder("");
		finder.append("select");
		finder.append("days");
		finder.append("from t_timeLimit");
		finder.append("where 1 = 1");
		finder.append("and clientId = :clientId").setParam("clientId", clientId);
		finder.append("and name like :name").setParam("name", cityName + "%");
		finder.append(" and st = 0");
		List<TimeLimit> list=baseDao.findList(finder,TimeLimit.class);
		if(list.size()>0){
			return list.get(0).getDays();
		}
		return 0;
	}
	
	// 动态查询
	public List<TimeLimit> findListBy(Page page,Map<String, Object> ffMap,I i){
		Finder finder = new Finder("");
		finder.append(" select");
		finder.append("count(1) as tt");
		finder.append(" from t_timeLimit t inner join t_client c on t.clientId = c.id");
		finder.append(" where 1=1");
		if(DaoUtils.isNotNull(ffMap.get("clientId"))){
			finder.append(" and t.clientId = :clientId").setParam("clientId", ffMap.get("clientId"));
		}
		if(DaoUtils.isNotNull(ffMap.get("name"))){
			finder.append(" and t.name like :name").setParam("name", "%" + ffMap.get("name") + "%");
		}
		if(DaoUtils.isNotNull(ffMap.get("st"))){
			finder.append(" and t.st = :st").setParam("st", ffMap.get("st"));
		}else{
			finder.append(" and t.st = 0");
		}
				
		finder.append("order by t.id desc");
		if (page != null) {
			Integer total = baseDao.findInt(finder);
			page.setTotal(total);
			finder.limit(page.getStart(), page.getOffset());
		}
		String sql = finder.getSql().replace("count(1) as tt","t.*,c.name as clientName");
		finder.setSql(sql);
		List<TimeLimit> list = baseDao.findList(finder, TimeLimit.class);				
		return list;
	}
}
